package kr.hkit.summary_java.serviceUI;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.summary_java.dao.DaoInterface;
import kr.hkit.summary_java.erp.dto.Department;

public class PanelDepartmentUI extends JPanel implements ActionListener {
	private JButton btnAdd;
	private InputPanel pDeptno;
	private InputPanel pDeptName;
	private InputPanel pFloor;

	private DaoInterface<Department> dao;
	/**
	 * Create the panel.
	 */
	public PanelDepartmentUI() {
		initialize();
	}
	
	public void setDao(DaoInterface<Department> dao) {
		this.dao = dao;
	}

	private void initialize() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel_Center = new JPanel();
		add(panel_Center);
		panel_Center.setLayout(new GridLayout(0, 1, 0, 0));
		
		pDeptno = new InputPanel("부서번호");
		panel_Center.add(pDeptno);
		
		pDeptName = new InputPanel("부서명");
		panel_Center.add(pDeptName);
		
		pFloor = new InputPanel("위치");
		panel_Center.add(pFloor);
		
		JPanel panel_Bottom = new JPanel();
		add(panel_Bottom, BorderLayout.SOUTH);
		
		btnAdd = new JButton("추가");
		btnAdd.addActionListener(this);
		panel_Bottom.add(btnAdd);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnAdd) {
			actionPerformedBtnAdd(e);
		}
	}
	
	protected void actionPerformedBtnAdd(ActionEvent e) {
		int dno = Integer.parseInt(pDeptno.getTf().trim());
		String name = pDeptName.getTf();
		int floor = Integer.parseInt(pFloor.getTf());
		
		Department d = new Department(dno, name, floor);
		JOptionPane.showMessageDialog(this, d);
		
		try {
			dao.insertItem(d);
			JOptionPane.showMessageDialog(this,"입력되었습니다");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this,"입력실패 다시 확인하세요.");
		}
	}
}
